package com.bw.module_house.view

import android.util.Log
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.module_house.R
import com.bw.module_house.adapter.HouseAdapter
import com.bw.module_house.adapter.ItemHouseAdapter
import com.bw.module_house.databinding.ActivityHouseBinding
import com.bw.module_house.viewmodel.HouseViewModel
/**
 * 智慧园区-03-房源信息V1.1-史建军-20250514
 */
@Route(path = RouterPath.PATH_HOUSE_ACTIVITY)
class HouseActivity : BaseActivity<ActivityHouseBinding>() {
    private val houseAdapter:HouseAdapter= HouseAdapter()
    private val itemAdapter:ItemHouseAdapter= ItemHouseAdapter(houseAdapter)

    private val vm by viewModels<HouseViewModel> {ViewModelProvider.NewInstanceFactory()  }
    override fun initView() {
        val data:MutableList<String> = mutableListOf()
        data.add("1栋")
        data.add("2栋")
        data.add("3栋")
        itemAdapter.data.clear()
        itemAdapter.data.addAll(data)
        vm.getHouseList()
        mDataBinding.topLayout.tvName.text="房源管理"
        mDataBinding.topLayout.iconBack.setOnClickListener {
            finish()
        }
        mDataBinding.topLayout.iconMore.setOnClickListener {
            val popupMenu=  PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_item,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.menu1->{
                        ARouter.getInstance().build(RouterPath.PATH_RENTING_HOUSE_ACTIVITY).navigation()
                        true
                    }
                    else->{
                        true
                    }
                }
            }
            popupMenu.show()
        }


        mDataBinding.rvHouse.adapter=itemAdapter
        mDataBinding.rvHouse.layoutManager=LinearLayoutManager(this)
        mDataBinding.btnOne.setOnClickListener {
            vm.getHouseListById(1)
            vm.houseIdSuccess.observe(this){
                Log.d("====", "initView: 111"+it.size)
                houseAdapter.data.clear()
                houseAdapter.data.addAll(it)
                houseAdapter.notifyDataSetChanged()
            }
        }
        mDataBinding.btnTwo.setOnClickListener {
            vm.getHouseListById(2)
            vm.houseIdSuccess.observe(this){
                houseAdapter.data.clear()
                houseAdapter.data.addAll(it)
                houseAdapter.notifyDataSetChanged()
            }
        }
        mDataBinding.btnThree.setOnClickListener {
            vm.getHouseListById(3)
            vm.houseIdSuccess.observe(this){
                houseAdapter.data.clear()
                houseAdapter.data.addAll(it)
                houseAdapter.notifyDataSetChanged()
            }
        }






        vm.houseSuccess.observe(this){
            val data=it.groupBy {
                it.regionName
            }.map {
                it.value.first()
            }

            houseAdapter.data.clear()
            houseAdapter.data.addAll(it)
            houseAdapter.notifyDataSetChanged()
        }



    }













    override fun getLayoutId(): Int =R.layout.activity_house

}